package com.lht.testDemo;

import org.junit.Test;

import java.util.*;

/**
 * @author Eric
 * @version 1.0
 * @date 2019-07-10
 */
public class TreeMapTest {
    /**
     * The maximum capacity, used if a higher value is implicitly specified
     * by either of the constructors with arguments.
     * MUST be a power of two <= 1<<30.
     */
    static final int MAXIMUM_CAPACITY = 1 << 30;

    @Test
    public void test1(){
        System.out.println(Integer.MAX_VALUE/60/60/24/365);
    }

    @Test
    public void test(){
        System.out.println(tableSizeFor(8));
    }

    static final int tableSizeFor(int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }

    @Test
    public void treeMapForeach(){
        TreeMap<Integer,String> treeMap=new TreeMap<>();
        treeMap.put(2,"22222");
        treeMap.put(3,"333");
        treeMap.put(1,"1111");
        //升序    sort
        for(Map.Entry<Integer,String> entry:treeMap.entrySet()){
            System.out.println(entry.getKey()+","+entry.getValue());
        }
        System.out.println("---------------------");

        //降序  sort
        for(Map.Entry<Integer,String> entry:treeMap.descendingMap().entrySet()){
            System.out.println(entry.getKey()+","+entry.getValue());
        }
        System.out.println("---------------------");

    }

    @Test
    public void treeSetForeach(){
        TreeSet<Integer> set=new TreeSet<Integer>();
        set.add(2);
        set.add(3);
        set.add(1);
        for(Integer i:set){
            System.out.println(i);
        }
        System.out.println("---------------");
        Spliterator<Integer> iterator=set.spliterator();
        iterator.forEachRemaining(k->{
            System.out.println(k);
        });
    }


}
